<template>
  <div class="form-setting-panel">
    <!-- start 标题 -->
    <div class="form-setting-group form-common-setting">
      <h3 class="form-setting-panel-title">{{ field.displayName }}</h3>
      <div class="form-design-warning">{{$t('common.form.preview.serviceItem.des2')}}</div>
    </div>
    <!-- end 标题 -->
    
    <!-- start 描述信息 -->
    <form-describe-setting
      :field="field"
      :mode="mode"
      @input="updateForDom"
    ></form-describe-setting>
    <!-- end 描述信息 -->
    
    <!-- start 校验 -->
    <div class="form-setting-group form-setting-item">
      <h4 class="form-item-title">{{$t('common.base.check')}}</h4>
      <div class="form-item-box">
        <!-- 必填 -->
        <form-required-setting :field="field" @input="update"></form-required-setting>
      </div>
    </div>
    <!-- end 校验 -->

    <!-- 结算规则设置 S -->
    <div class="form-setting-group form-common-setting" v-if="isShowSettlementRules">
      <h4 class="form-item-title">{{ $t('zhongqi.setting.text2') }}</h4>
      <div class="form-item-box">{{ $t('zhongqi.setting.text3') }}</div>
      <settlement-rules ref="settlementRules" @saveRule="saveRule" :fields="fields" :field="field" />
    </div>
    <!-- 结算规则设置 E -->

    <!-- 服务项目折扣设置 S -->
    <div class="form-setting-group form-common-setting" v-if="isServiceItemDiscountSettings">
      <h4 class="form-item-title">{{ $t('kaili.setting.serviceItemDiscountSettings') }}</h4>
      <el-button type="primary" @click="handleDiscountRules">{{ $t('kaili.setting.viewDiscountRules') }}</el-button>
    </div>
    <!-- 服务项目设置 E -->

    <!-- 其他设置 start -->
    <div class="form-setting-group form-setting-item" v-if="allowPublicSet">
      <h4 class="form-item-title">{{$t('common.base.otherSet')}}</h4>
      <!-- 仅可从服务清单中添加 start -->
      <div class="form-item-box" v-if="isTaskServiceList">
        <el-checkbox :value="field.setting.onlyFromServiceItem" @input="updateSetting(...arguments,'onlyFromServiceItem')">
          {{$t('common.form.tip.serviceItem.tips2')}}
        </el-checkbox>
      </div>
      <!-- 仅可从服务清单中添加 end -->
      <div class="form-item-box">
        <el-checkbox v-if="isAreaPrice" :value="field.setting.useRegionalPricing" @input="updateSetting(...arguments,'useRegionalPricing')">
          {{$t('common.base.useRegionalPricing')}}
        </el-checkbox>
      </div>
      <!-- 是否仅展示关联产品的服务项目内容  -->
      <div class="form-item-box" v-if="isTaskServiceList">
        <el-checkbox :value="field.setting.onlyShowRelatedServiceItem" @input="updateSetting(...arguments,'onlyShowRelatedServiceItem')">
          {{$t('common.form.tip.serviceItem.tips3')}}
        </el-checkbox>
      </div>
    </div>
    <!-- 其他设置 end -->

    <discount-rules  ref="discountRulesRef" :field="field" @submit="updateSetting(...arguments,'serviceItemDiscountSettings')"></discount-rules>
  </div>
</template>

<script>
import SettingMixin from '@src/component/form/mixin/setting';

import SettlementRules from '@src/component/form/components/FormServiceIterm/components/SettlementRules.vue';
import DiscountRules from '@src/component/form/components/FormServiceIterm/components/DiscountRules.vue';

import { settingProps } from '@src/component/form/components/props';
import { getRootWindow } from '@src/util/dom';

export default {
  name: 'form-serviceterm-setting',
  mixins: [SettingMixin],
  props: settingProps,
  components: {
    [DiscountRules.name]: DiscountRules,
    [SettlementRules.name]: SettlementRules
  },
  data() {
    return {
    }
  },
  computed: {
    // 服务清单灰度
    isTaskServiceList() {
      const RootWindow = getRootWindow(window)
      return RootWindow.grayAuth?.taskServiceList || false
    },
    isShowSettlementRules() {
      const RootWindow = getRootWindow(window)
      return RootWindow.grayAuth?.settlementRule || false
    }, 
    // 服务项目折扣灰度
    isServiceItemDiscountSettings() {
      const RootWindow = getRootWindow(window)
      return Boolean(RootWindow.grayAuth?.serviceItemDiscountSettings)
    },
    // 是否开启区域定价灰度
    isAreaPrice() {
      const RootWindow = getRootWindow(window)
      return RootWindow.grayAuth?.areaPrice
    },
  },
  methods: {
    handleDiscountRules(){
      this.$refs.discountRulesRef.open();
    },
    // 更新setting
    updateSetting(value, type){
      this.$set(this.field.setting, type, value)
    },

    updateForDom(event){
      let el = event.target;
      let prop = el.dataset.prop;
      let value = el.value;
      
      this.update(value, prop)
    },
    update(value, prop, isSetting = false) {
      this.$emit('input', {value, prop, isSetting});
    },
    // 保存质保规则
    saveRule(value) {
      this.update(value, 'ruleList', true);
    },
  }
}
</script>
<style lang="scss" scoped>
</style>
